#include<stdio.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>

int main()
{
    //close(1);//关闭标准输入：显示器stdout
    umask(0);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    //int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0666);
    int fd = open("log.txt", O_RDONLY);//读的形式
    if(fd < 0)
    {
        perror("open");
        return 1;
    }
    
    dup2(fd, 0); //输入重定向

    char line[64];
    while(1)
    {
        printf(">");
        if(fgets(line, sizeof(line), stdin) == NULL) break;
        printf("%s", line);
    }

    //dup2(fd, 1);

    //printf("open fd: %d\n", fd);
    //fprintf(stdout, "open fd: %d\n", fd); //printf->stdout
    //const char* msg = "hello world";
    //write(1, msg, strlen(msg));// 1是内核的输出，但是被替换到了log.txt

    //fflush(stdout);
    close(fd);
    return 0;
}
